package com.whu.exam.modules.sys.classno.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.whu.exam.core.api.ApiRest;
import com.whu.exam.core.api.controller.BaseController;
import com.whu.exam.core.api.dto.BaseIdReqDTO;
import com.whu.exam.core.api.dto.BaseIdsReqDTO;
import com.whu.exam.core.api.dto.PagingReqDTO;
import com.whu.exam.core.utils.BeanMapper;
import com.whu.exam.modules.sys.classno.dto.request.ClassnoSortReqDTO;
import com.whu.exam.modules.sys.classno.dto.response.SysClassnoTreeDTO;
import com.whu.exam.modules.sys.classno.entity.SysClassno;
import com.whu.exam.modules.sys.classno.service.SysClassnoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 班级信息控制器
 * </p>
 */
@Api(tags={"班级信息"})
@RestController
@RequestMapping("/exam/api/sys/classno")
public class SysClassnoController extends BaseController {

    @Autowired
    private SysClassnoService baseService;

    /**
     * 添加或修改
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "添加或修改")
    @RequestMapping(value = "/save", method = { RequestMethod.POST})
    public ApiRest save(@RequestBody SysClassno reqDTO) {
        baseService.save(reqDTO);
        return super.success();
    }

    /**
     * 批量删除
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "批量删除")
    @RequestMapping(value = "/delete", method = { RequestMethod.POST})
    public ApiRest edit(@RequestBody BaseIdsReqDTO reqDTO) {
        //根据ID删除
        baseService.removeByIds(reqDTO.getIds());
        return super.success();
    }

    /**
     * 查找详情
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "查找详情")
    @RequestMapping(value = "/detail", method = { RequestMethod.POST})
    public ApiRest<SysClassno> find(@RequestBody BaseIdReqDTO reqDTO) {
        SysClassno entity = baseService.getById(reqDTO.getId());
        SysClassno dto = new SysClassno();
        BeanUtils.copyProperties(entity, dto);
        return super.success(dto);
    }

    /**
     * 分页查找
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "分页查找")
    @RequestMapping(value = "/paging", method = { RequestMethod.POST})
    public ApiRest<IPage<SysClassnoTreeDTO>> paging(@RequestBody PagingReqDTO<SysClassno> reqDTO) {

        //分页查询并转换
        IPage<SysClassnoTreeDTO> page = baseService.paging(reqDTO);

        return super.success(page);
    }

    /**
     * 查找列表，每次最多返回200条数据
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "查找列表")
    @RequestMapping(value = "/list", method = { RequestMethod.POST})
    public ApiRest<List<SysClassno>> list(@RequestBody SysClassno reqDTO) {

        //分页查询并转换
        QueryWrapper<SysClassno> wrapper = new QueryWrapper<>();

        //转换并返回
        List<SysClassno> list = baseService.list(wrapper);

        //转换数据
        List<SysClassno> dtoList = BeanMapper.mapList(list, SysClassno.class);

        return super.success(dtoList);
    }


    /**
     * 树列表
     * @return
     */
    @ApiOperation(value = "树列表")
    @RequestMapping(value = "/tree", method = { RequestMethod.POST})
    public ApiRest<List<SysClassnoTreeDTO>> tree() {
        List<SysClassnoTreeDTO> dtoList = baseService.findTree();
        return super.success(dtoList);
    }


    /**
     * 分类排序
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "分类排序")
    @RequestMapping(value = "/sort", method = { RequestMethod.POST})
    public ApiRest sort(@RequestBody ClassnoSortReqDTO reqDTO) {
        baseService.sort(reqDTO.getId(), reqDTO.getSort());
        return super.success();
    }
}
